package tv.huan.media.rabbit;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DirectExchangeConfig {

    @Bean
    public Queue queue() {
        //true表示持久化该队列
        return new Queue(RabbitConstant.EMAIL_QUEUE, true);
    }

    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(RabbitConstant.DIRECT_EMAIL_EXCHANGE);
    }

    @Bean
    public Binding binding() {
        return BindingBuilder.bind(queue()).to(directExchange()).with(RabbitConstant.EMAIL_ROUTING);
    }

    @Bean
    public Queue cardPoolEmailqueue() {
        //true表示持久化该队列
        return new Queue(RabbitConstant.CARD_POOL_EMAIL_QUEUE, true);
    }

    @Bean
    public DirectExchange cardPoolEmaildirectExchange() {
        return new DirectExchange(RabbitConstant.DIRECT_CARD_POOL_EMAIL_EXCHANGE);
    }

    @Bean
    public Binding cardPoolEmailbinding() {
        return BindingBuilder.bind(cardPoolEmailqueue()).to(cardPoolEmaildirectExchange()).with(RabbitConstant.CARD_POOL_EMAIL_ROUTING);
    }

    @Bean
    public SimpleRabbitListenerContainerFactory pointTaskContainerFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer,
                                                                          ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        //一个消费者每次取消息的数量
        factory.setPrefetchCount(5);
        //设置消费者个数
        factory.setConcurrentConsumers(20);
        configurer.configure(factory, connectionFactory);
        return factory;
    }

    //开启手动 ack
    @Bean
    public RabbitListenerContainerFactory cooontainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        //每个消费者每次监听时可拉取处理的消息数量
        factory.setPrefetchCount(5);
        //并发消费者初始化值
        factory.setConcurrentConsumers(50);
        factory.setConnectionFactory(connectionFactory);
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        return factory;
    }
}
